﻿@model CategoryProductModel

@{
	var parentModel = ViewData["Parent"] as CategoryModel;
	var showRuleApplyButton = (bool)ViewBag.ShowRuleApplyButton;
}

<datagrid id="product-categories-grid"
          key-member="Id"
          allow-edit="true"
          allow-resize="true"
          allow-row-selection="true"
          allow-column-reordering="true"
          preserve-grid-state="true"
          onrowclass="productCategoriesGrid_onRowClass">
    <datasource read="@Url.Action("ProductCategoryList", new { categoryId = parentModel.Id })" 
                update="@Url.Action("ProductCategoryUpdate")"
                delete="@Url.Action("ProductCategoryDelete")" />
    <paging position="Bottom" show-size-chooser="true" />
    <sorting allow-unsort="true" allow-multisort="false" />
    <toolbar>
        <toolbar-group>
            <button id="assign-products-to-categories" type="button" class="btn btn-primary btn-flat no-anims mr-auto">
                <i class="fa fa-plus"></i>
                <span>@T("Admin.Catalog.Categories.Products.AddNew")</span>
            </button>
            <button datagrid-action="DataGridToolAction.SaveChanges" type="button" class="btn btn-warning no-anims btn-flat">
                <i class="fa fa-check"></i>
                <span>@T("Admin.Common.SaveChanges")</span>
            </button>
            <button datagrid-action="DataGridToolAction.CancelEdit" type="button" class="btn btn-light btn-flat mr-auto">
                <span>@T("Common.Cancel")</span>
            </button>
        </toolbar-group>
        @if (showRuleApplyButton)
        {
            <toolbar-group>
                <button type="button" id="reapply-rules" class="btn btn-primary btn-flat" data-url="@Url.Action("ApplyRules", new { id = parentModel.Id })">
                    <i class="fa fa-play"></i>
                    <span>@T("Admin.Rules.ReapplyRules")</span>
                </button>
            </toolbar-group>
        }
        <zone name="datagrid_toolbar_alpha"></zone>
        <toolbar-group class="omega"></toolbar-group>
        <zone name="datagrid_toolbar_omega"></zone>
        <toolbar-group>
            <button datagrid-action="DataGridToolAction.DeleteSelectedRows" type="button" class="btn btn-danger no-anims btn-flat">
                <i class="far fa-trash-can"></i>
                <span>@T("Admin.Common.Delete.Selected")</span>
            </button>
        </toolbar-group>
    </toolbar>
    <row-commands>
        <a datarow-action="DataRowAction.InlineEdit">@T("Common.Edit")</a>
        <a datarow-action="DataRowAction.Delete">@T("Common.Delete")</a>
    </row-commands>
    <columns>
        <column for="Id" halign="center" visible="false" type="string" readonly="true" />
		<column for="ProductId" width="3fr" hideable="false" entity-member="Product.Name">
            <display-template>
                @Html.LabeledProductName(valueExpression: "item.row.ProductName")
            </display-template>
		</column>
        <column for="Sku" readonly entity-member="Product.Sku" />
        <column for="Published" readonly entity-member="Product.Published" halign="center" />
        <column for="IsFeaturedProduct" halign="center" />
        <column for="DisplayOrder" halign="center" />
        <column for="IsSystemMapping" readonly halign="center" />
    </columns>
</datagrid>

<entity-picker icon-css-class="fa fa-plus"
               dialog-title="@T("Admin.Catalog.Categories.Products.AddNew")"
               onselectioncompleted="assignProductsToCategory"
               class="assign-products-picker hide" />

<script sm-target-zone="scripts" data-origin="product-categories-grid">
    $(function () {
        $(document).on('click', '#assign-products-to-categories', () => {
            $('.assign-products-picker').trigger('click');
            return false;
        });
    });

    function productCategoriesGrid_onRowClass(row) {
        return {
            "text-muted": !row.Published
        };
    }

    function assignProductsToCategory(ids) {
        $({}).ajax({
            type: 'POST',
            url: '@Url.Action("AssignProducts")',
            data: {
                "categoryId": @parentModel.Id,
                "selectedProductIds": ids
            },
            success: function () {
                window["product-categories-grid"].$children[0].read();
                //$('#entpicker-product-dialog').entityPicker('clearSelection');
            }
        });
        return true;
    }
</script>